<?php
use \Illuminate\Support\Facades\Cache;

use App\Models\Comment;
//评论
if (!function_exists('show_comments')){
    function show_comments($write_id,$open=1){  //0看全部的(管理员防撤回)   1看没有删掉的
        $comments=Comment::where('write_id',$write_id)->
        select(['id','pid','user_id','subs','created_at','is_locked','write_id','touser_id'])
            ->where('pid',0)
            ->with([
                'user:id,name,avatar'
            ])
            ->with(['write:id,title'])
            ->with([
                'children'=>function($query)use($open){
                    $query->when($open !== 1,function ($query)use($open){
                        $query->where('is_locked',$open);//二层
                    } )->with(['user:id,name,avatar'])->with(['touser:id,name,avatar'])->with(['write:id,title']);
                },
            ])->get();
        return $comments;
    }
}


//缓存 没有删除的评论
if(!function_exists('cache_show_comments')){
    function cache_show_comments($write_id){
       return Cache::rememberForever('cache_show_comments',function ()use($write_id){
          return show_comments($write_id);
       });
    }
}

//缓存 删除的+没有删除的
if(!function_exists('cache_show_comments_all')){
    function cache_show_comments_all($write_id){
        return Cache::rememberForever('cache_show_comments_all',function ()use($write_id){
            return show_comments($write_id,0);
    });
}
}



//清空 缓存
if(!function_exists('cache_show_comments_forget')){
    function cache_show_comments_forget(){
      Cache::forget('cache_show_comments_all');
      Cache::forget('cache_show_comments');
    }
}



//oss
if(!function_exists('oss')){
    function oss($key){
       return config('filesystems')['disks']['oss']['bucket_url'].$key;
    }
}
